package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Page;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TypeTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("type")
public class TypeTemplateController {

    @Reference
    private TypeTemplateService typeTemplateService;

    @Reference
    private BrandService brandService;

    @Reference
    private SpecificationService specificationService;

    @RequestMapping("list")
    public String list(Model model){
        List<TbBrand> list = brandService.brandList();
        List<TbSpecification> spList = specificationService.getSpList();
        model.addAttribute("list",list);
        model.addAttribute("spList",spList);
        return "admin/type_template";
    }


    //查询
    @RequestMapping("templatePage")
    @ResponseBody
    public Result templatePage(@RequestParam("name") String name,@RequestParam(defaultValue = "1")Integer pageNumber,@RequestParam(defaultValue = "3")Integer pageSize){
        try {
            Page<TbTypeTemplate> page = typeTemplateService.templatePage(name,pageNumber, pageSize);
            return Result.response(ResultEnum.SUCCESS).setData(page);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //增加
    @RequestMapping("addInfo")
    @ResponseBody
    public Result addInfo(TbTypeTemplate tbTypeTemplate){
        try {
            Integer i = typeTemplateService.addInfo(tbTypeTemplate);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //回显
    @RequestMapping("getTypeById")
    @ResponseBody
    public Result getTypeById(@RequestParam("id") Long id) {
        try {
            TbTypeTemplate type = typeTemplateService.getTypeById(id);
            return Result.response(ResultEnum.SUCCESS).setData(type);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //修改
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbTypeTemplate tbTypeTemplate){
        try {
            Integer i = typeTemplateService.updateInfo(tbTypeTemplate);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //批量删除
    @RequestMapping("deleteTypeById")
    @ResponseBody
    public Result deleteTypeById(@RequestParam("id[]")Long[] id){
        try {
            Integer i = typeTemplateService.deleteTypeById(id);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //删除
    @RequestMapping("deleteById")
    @ResponseBody
    public Result deleteById(Long id){
        try {
            Integer i = typeTemplateService.deleteById(id);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }
}
